﻿using AutoMapper;
using Dapper;
using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.RBACs;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using MySql.Data.MySqlClient;
using System.Data;

namespace RBAC.Write.API.Applications.PermissionsFiles
{
    public class UserCreateHander : IRequestHandler<UserCreateCommand, ResultsDTO>
    {
        private readonly IBateRepository<Users> user;
        private readonly IMapper mapper;

        public UserCreateHander(IBateRepository<Users> user, IMapper mapper)
        {
            this.user = user;
            this.mapper = mapper;
        }

        public async Task<ResultsDTO> Handle(UserCreateCommand request, CancellationToken cancellationToken)
        {

            ResultsDTO res = new ResultsDTO() { Code = 0 };
            Users users = new Users()
            {
                UserId = request.UserId,
                UserName = request.UserName,
                PassWord = request.PassWord,
                State = request.State,
            };
            DapperHelper dapperHelper = new DapperHelper();
            string sql = "insert User values(@UserId,@UserName,@PassWord,@State)";
            string sql1 = "select Max(UserId) from user";
            string sql2 = "insert userroles values(@UserRolesId,@UserId,@RolesId)";
            string sql3 = "select COUNT(*) from User WHERE UserName='"+request.UserName+"'";
            using (MySqlConnection conn = new MySqlConnection(dapperHelper.RBACWrite))
            {
                conn.Open();
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        var cou = conn.Query<int>(sql3, users,tran).FirstOrDefault();
                        if (cou > 0)
                        {
                            res.Message = "该用户已存在";
                            return res;
                        }
                        res.Code += await conn.ExecuteAsync(sql, users, tran);
                        var userId = conn.Query<int>(sql1, users, tran).FirstOrDefault();
                        foreach (var item in request.Ids)
                        {
                            UserRoles userRoles = new UserRoles()
                            {
                                UserRolesId = 0,
                                UserId = userId,
                                RolesId=item
                            };
                            res.Code += await conn.ExecuteAsync(sql2, userRoles, tran);
                        }
                        tran.Commit();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        throw;
                    }
                }
                if (res.Code > 0)
                {
                    res.Message = "添加成功";
                }
                return res;
            }
        }
    }
}


